﻿
using System.Text.RegularExpressions;
namespace TTS
{
    /// <summary>
    ///SNHelper 的摘要说明
    /// </summary>
    public static class SNHelper
    {
        /// <summary>
        /// SN检查方法
        /// </summary>
        /// <param name="SN"></param>
        /// <param name="TCode"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool SNCheck(string SN, string TCode, out string message)
        {
            string[] sns = SN.Split(' ');
            if (CheckBlanks(sns, out message))
                if (CheckTCode(sns[0], TCode, out message))
                    if (CheckSNLength(sns[2], out message))
                        if (CheckVendor(sns[2].Substring(4, 1), out message))
                            if (CheckYear(sns[2].Substring(0, 2), out message))
                                if (CheckWeek(sns[2].Substring(2, 2), out message))
                                    return CheckSerilNo(sns[2].Substring(5, 4), out message);
            message = TCode + "的" + message;
            return false;
        }

        /// <summary>
        /// 检查MAC格式的方法
        /// </summary>
        /// <param name="SNMac"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool SNMacCheck(string SNMac,out string message)
        {
            string[] SNMacArray = SNMac.Split(':');
            for(int i=0;i<SNMacArray.Length;i++)
            {
                if(CheckMacSubString(SNMacArray[i],out message))
                {
                    continue;
                }
                else
                {
                   return false;
                }
            }
            message = "";
            return true;
        }

        /// <summary>
        /// KccUp标签号检查方法
        /// </summary>
        /// <param name="KccUpSN"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool KccUpSNCheck(string KccUpSN, out string message)
        {
            if (CheckKccUpOrDownSnLength(KccUpSN, out message))
            {
                if (CheckManuCustomer(KccUpSN, out message))
                {
                    if (CheckKccUpSerilNo(KccUpSN, out message))
                    {
                        message = "";
                        return true;
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// KccDown标签号检查方法
        /// </summary>
        /// <param name="KccDownSN"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool KccDownSNCheck(string KccDownSN, out string message)
        {
            if (CheckKccUpOrDownSnLength(KccDownSN, out message))
            {
                if (CheckBlankKccDown(KccDownSN, out message))
                {
                    if (CheckPECFormat(KccDownSN, out message))
                    {
                        if (CheckRelease(KccDownSN, out message))
                        {
                            message = "";
                            return true;
                        }
                    }
                }

            }
            return false;
        }

        /// <summary>
        /// SN版本号比对方法
        /// </summary>
        /// <param name="SN"></param>
        /// <param name="version"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool SNCheckVersion(string SN,string version,out string message)
        {
            string[] SNArray = SN.Split(' ');
            if(SNArray[1]==version)
            {
                message = "";
                return true;
            }
            message = "条码版本号有问题";
            return false;
        }
        public static bool SNCheckVersionTwo(string SN, string version, out string message)
        {
            string[] SNArray = SN.Split(' ');
            string[] SNVersion = version.Split(',');
            if (SNArray[1] == SNVersion[0] || SNArray[1] == SNVersion[1])
            {
                message = "";
                return true;
            }
            message = "条码版本号有问题";
            return false;
        }
        public static bool SNCheckVersionThree(string SN, string version, out string message)
        {
            string[] SNArray = SN.Split(' ');
            string[] SNVersion = version.Split(',');
            if (SNArray[1] == SNVersion[0] || SNArray[1] == SNVersion[1] || SNArray[1] == SNVersion[2])
            {
                message = "";
                return true;
            }
            message = "条码版本号有问题";
            return false;
        }
      
        public static bool SNCheckVersionRandom(string SN, string version, out string message)
        {
            string[] SNArray = SN.Split(' ');
            string[] SNVersion = version.Split(',');
            for (int i = 0; i < SNVersion.Length;i++ )
            {
                if (SNArray[1] == SNVersion[i])
                {
                    message = "";
                    return true;
                }
            }                
            message = "条码版本号有问题";
            return false;
        }

        /// <summary>
        /// Kcc标签版本号比对方法
        /// </summary>
        /// <param name="KccDownSn"></param>
        /// <param name="version"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool SNCheckKccDownVersion(string KccDownSn,string version,out string message)
        {
            string[] KccDownSnArray=KccDownSn.Split(' ');
            if(KccDownSnArray[2]==version)
            {
                message = "";
                return true;
            }
            message = "条码版本号有问题";
            return false;
        }

        /// <summary>
        /// Kcc标签版本号比对方法(重载)，后续需要重构，把版本管理统一到版本管理表中
        /// </summary>
        /// <param name="KccDownSn"></param>
        /// <param name="version"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool SNCheckKccDownVersion(string KccDownSn, string[] version, out string message)
        {
            string[] KccDownSnArray = KccDownSn.Split(' ');          
            for (int i = 0; i < version.Length; i++)
            {
                if (KccDownSnArray[2] == version[i])
                {
                    message = "";
                    return true;
                }
            }                
            message = "条码版本号有问题";
            return false;
        }
        public static bool CheckKccDownPec(string KccDownSn, string PEC, out string message)
        {
            string[] KccDownSnArray = KccDownSn.Split(' ');
            if (KccDownSnArray[0] == PEC)
            {
                message = "";
                return true;
            }
            message = "条码PEC有问题";
            return false;
        }
        /// <summary>
        /// 重载PEC的判断，后面要重构PEC的判断，因为PEC也是一个变化的值
        /// </summary>
        /// <param name="KccDownSn"></param>
        /// <param name="PEC"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public static bool CheckKccDownPec(string KccDownSn, string[] PEC, out string message)
        {
            string[] KccDownSnArray = KccDownSn.Split(' ');
            for (int i = 0; i < PEC.Length;i++ )
            {
                if (KccDownSnArray[0] == PEC[i])
                {
                    message = "";
                    return true;
                }
            }
               
            message = "条码PEC有问题";
            return false;
        }
        private static bool CheckBlanks(string[] sns, out string message)
        {
            if (sns.Length == 3)
            {
                message = "";
                return true;
            }
            message = "条码格式错误，请检查空格数";
            return false;
        }

        private static bool CheckTCode(string sn, string TCode, out string message)
        {
            if (sn == TCode)
            {
                message = "";
                return true;
            }
            message = "条码格式错误，请检查TCode";
            return false;
        }

        private static bool CheckSNLength(string sn, out string message)
        {
            if (sn.Length == 9)
            {
                message = "";
                return true;
            }
            message = "条码格式错误，请检查条码长度";
            return false;
        }

        private static bool CheckVendor(string sn, out string message)
        {
            if (sn == "M")
            {
                message = "";
                return true;
            }
            message = "条码格式错误，请检查供应商代码";
            return false;
        }

        private static bool CheckYear(string sn, out string message)
        {
            int year;
            if (int.TryParse(sn, out year))
            {
                if (year > 13 && year < 20)
                {
                    message = "";
                    return true;
                }
            }
            message = "条码格式错误，请检查年份";
            return false;
        }

        private static bool CheckWeek(string sn, out string message)
        {
            int week;
            if (int.TryParse(sn, out week))
            {
                if (week > 0 && week < 54)
                {
                    message = "";
                    return true;
                }
            }
            message = "条码格式错误，请检查周数";
            return false;
        }

        private static bool CheckSerilNo(string sn, out string message)
        {
            int serialno;
            if (int.TryParse(sn, out serialno))
            {
                message = "";
                return true;
            }
            message = "条码格式错误，请检查流水号";
            return false;
        }

        private static bool CheckKccUpOrDownSnLength(string KccUpSn,out string message)
        {
            if(KccUpSn.Length==12)
            {
                message = "";
                return true;
            }
            message = "条码长度有误";
            return false;
        }
        private static bool CheckManuCustomer(string KccUpSn,out string message)
        {
            if(KccUpSn.Substring(0,6)=="KCCA10")
            {
                message = "";
                return true;
            }
            message = "条码格式中公司名称及摩比在客户处的代码有误";
            return false;
        }
        private static bool CheckKccUpSerilNo(string KccUpSn,out string message )
        {
           /* string valEx = @"^[0-9]*$ ";
            if(Regex.IsMatch( KccUpSn.Substring(6,6),valEx) )
            {
                message = "";
                return true;
            }
            message = "生产流水号格式有问题";
            return false;*/

            foreach (char singleChar in KccUpSn.Substring(6, 6))
            {
                if (singleChar >= '0' && singleChar <= '9')
                {
                    /*message = "";
                    return true;*/
                    continue;
                }
                else
                {
                    message = "生产流水号格式有问题";
                    return false;
                }
            }
            message = "";
            return true;
        }

        private static bool CheckPECFormat(string KccDownSn,out string message)
        {
          /*  string valEx = @"^[A-Z0-9]+$";
            if(Regex.IsMatch(KccDownSn.Substring(0,8),valEx))
            {
                message = "";
                return true;
            }
            message = "条码PEC格式有问题";
            return false;*/
            foreach (char singleChar in KccDownSn.Substring(0, 8))
            {
                if((singleChar>='0'&&singleChar<='9')||(singleChar>='A'&&singleChar<='Z'))
                {
                    /*message = "";
                    return true;*/
                    continue;
                }
                else
                {
                    message = "条码PEC格式有问题";
                    return false;
                }
            }
            message = "";
            return true;
        }

        private static bool CheckBlankKccDown(string KccDownSn,out string message)
        {
            if(KccDownSn.IndexOf(' ')==8&&KccDownSn.LastIndexOf(' ')==9)
            {
                message = "";
                return true;
            }
            message = "条码空格数有问题";
            return false;
            
        }
        private static bool CheckRelease(string KccDownSn,out string message)
        {          
            string[] KccDownSnArray = KccDownSn.Split(' ');
            foreach (char singleChar in KccDownSnArray[2])
            {
                if ((singleChar >= '0' && singleChar <= '9') || (singleChar >= 'A' && singleChar <= 'Z'))
                {                  
                    continue;
                }
                else
                {
                    message = "条码release中的数字字符或字母字符有问题";
                    return false;
                }
            }
            message = "";
            return true;
        }

        private static bool CheckMacSubString(string MacSubString,out string message)
        {
            foreach(char singleChar in MacSubString)
            {
                if((singleChar>='0'&&singleChar<='9')||(singleChar>='A'&&singleChar<='F'))
                {
                    continue;
                }
                else
                {
                    message = "条码格式中数字字符或者字母字符有问题";
                    return false;
                }
            }
            message = "";
            return true;
        }

        private static bool CheckTCodeSnLength(string TcodeSN, out string message)
        {
            if (TcodeSN.Length == 20)
            {
                message = "";
                return true;
            }
            message = "条码长度有误";
            return false;
        }

        private static bool CheckBlankTcode(string TcodeSn, out string message)
        {
            if (TcodeSn.IndexOf(' ') == 8 && TcodeSn.LastIndexOf(' ') == 10)
            {
                message = "";
                return true;
            }
            message = "条码空格数有问题";
            return false;

        } 

        public static bool TCodeSNCheck(string TcodeSN, out string message)
        {
            if (CheckTCodeSnLength(TcodeSN, out message))
            {
                if (CheckBlankTcode(TcodeSN, out message))
                { 
                      message = "";
                      return true;
                }

            }
            return false;
        }
    }
}
